<template>
    <div class="main">
        <el-card class="box-card">
            <el-form :inline="true" :model="page" class="demo-form-inline">
                <el-form-item label="用户">
                    <el-input v-model="page.userName" placeholder="请输入昵称" clearable />
                </el-form-item>
                <el-form-item label="操作员">
                    <el-input v-model="page.operator" placeholder="请输入昵称" clearable />
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="page.status" placeholder="请选择" clearable>
                        <el-option label="未支付" value="未支付" />
                        <el-option label="已支付" value="已支付" />
                        <el-option label="已退款" value="已退款" />
                    </el-select>
                </el-form-item>
                <!-- <el-form-item label="类型">
                    <el-select v-model="page.type" placeholder="请选择" clearable>
                        <el-option label="积分" value="积分" />
                        <el-option label="余额" value="余额" />
                    </el-select>
                </el-form-item> -->
                <el-form-item label="时间">
                    <el-date-picker v-model="page.time" type="daterange" range-separator="至" start-placeholder="开始时间"
                        end-placeholder="结束时间" clearable format="YYYY/MM/DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"/>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">搜索</el-button>
                    <el-button type="primary" @click="reset">重置</el-button>
                </el-form-item>
            </el-form>

            <el-table :data="tableData" style="width: 100%" max-height="600px"
                @selection-change="handleSelectionChange">
                <el-table-column prop="userName" label="用户" align="center">
                </el-table-column>
                <el-table-column prop="payMoney" label="支付金额" align="center">
                </el-table-column>
                <el-table-column prop="payType" label="支付方式" align="center">
                </el-table-column>
                <!-- <el-table-column prop="type" label="充值类型" align="center">
                </el-table-column> -->
                <el-table-column prop="balanceReceived" label="到账余额" align="center">
                </el-table-column>
                <el-table-column prop="createTime" label="时间" align="center">
                </el-table-column>
                <el-table-column prop="operator" label="操作员" align="center">
                </el-table-column>
                <el-table-column prop="status" label="状态" width="160">
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="160">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="addEdiClick(2, scope.row)">继续充值</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination v-if="total != 0" :page="page" :total="total" @pageSizeChanged="handlePageSizeChange"
                @currentPageChanged="handleCurrentPageChange" />
        </el-card>
    </div>
    <!-- 新增修改 -->
    <el-dialog v-model="dialogVisible" :title="title" width="400px" :before-close="handleClose" top="30vh">
        <div>
            <el-form :model="formpage">
                <el-form-item label="数量">
                    <el-input v-model="formpage.payMoney" placeholder="请输入" />
                </el-form-item>
            </el-form>
        </div>
        <template #footer>
            <span class="dialog-footer">
                <el-button type="primary" @click="saveData">
                    保存
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { rechargeRecord, rechargePointsBalance } from '@/api/userManagement.js'
import { onMounted, reactive, ref } from 'vue'
import Pagination from '@/components/pagination/index.vue' // 引入分页组件
import { ElMessage } from 'element-plus'

const dialogVisible = ref(false)
const page = reactive({
    userName: "",
    operator: "",
    type: '',
    status: '',
    time: [
    ],
    pageNum: 1,
    pageSize: 10
})
const formpage = reactive({
    payMoney: 0,
})
const total = ref(0)
const tableData = reactive([])
const rowData = reactive({
    row: {}
})
const multipleSelection = ref([])
const title = ref('')

// 搜索
const onSubmit = () => {
    getList()
}
// 重置
const reset = () => {
    page.userName = "";
    page.operator = "";
    page.type = '';
    page.status = '';
    page.time = [];
    page.pageNum = 1;
    page.pageSize = 10;
    getList()
}
// 分页
const handlePageSizeChange = (val) => {
    page.pageNum = 1
    page.pageSize = val
    getList()
}

const handleCurrentPageChange = (val) => {
    page.pageNum = val
    // page.pageSize = 10
    getList()
}

// 弹框关闭
const handleClose = () => {
    dialogVisible.value = false
}
// 查看
const addEdiClick = (num, row) => {
    dialogVisible.value = true
    rowData.row = row
    title.value = row.type
}
// 多选
const handleSelectionChange = (val) => {
    multipleSelection.value = val
    console.log(val);

}
// 保存
const saveData = () => {
    let par = {
        ...formpage, loginId: JSON.parse(localStorage.getItem('userInfo')).data.id, userId: rowData.row.id, type: rowData.row.type
    }
    rechargePointsBalance(par).then(res => {
        if (res.code == 200) {
            ElMessage({
                message: res.msg,
                type: 'success'
            });
            dialogVisible.value = false
            getList()
        }
    })
}

onMounted(() => {
    getList()
})
// 获取列表
const getList = () => {
    tableData.length = 0
    total.value = 0
    rechargeRecord(page).then(res => {
        if (res.code == 200) {
            tableData.push(...res.data.records)
            total.value = res.data.total
        }
    })
}
</script>

<style lang="scss" scoped>
.demo-form-inline {
    width: 100%;
    min-width: 1000px;
    word-wrap: normal;

    ::v-deep .el-date-editor {
        width: 330px !important;
    }
}

.box-card :deep(.el-select__wrapper) {
    width: 100px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

.main {
    padding: 0 5px 5px 5px;
}
</style>